package com.example.b_308.myapplication.controller;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.comment.utils.Global;
import com.example.b_308.myapplication.controller.base.BaseController;
import com.example.b_308.myapplication.model.base.BaseCompany;
import com.example.b_308.myapplication.service.IUserView;
import com.example.b_308.myapplication.service.UserService;
import com.example.b_308.myapplication.model.Staff;

/**
 * Created by B-308 on 2017/11/29.
 */
public class UserController extends BaseController {
      IUserView iUserView;
      UserService userService;

    public UserController(IUserView iUserView) {
        this.iUserView = iUserView;
        userService =new UserService(iUserView);
        userService.loginDefault();
    }
    public void loginDefault(){
        userService.loginDefault();
    }

    public void login(String phone, String password){

        if(!PhoneValidator.isPhoneNumber(phone)){
            iUserView.onFail(Global.getString(R.string.phone_error));
        }
        if (password.length()<6||password.length()>13){
            iUserView.onFail(Global.getString(R.string.password_error));
        }
        Staff staff =new Staff();
        staff.setPhone(phone);
        staff.put("password",password);
        userService.login(staff);
    }
    public void register(BaseCompany company, Staff staff){
//        staff.putAttrs(company);
        userService.register(staff);
    }

    public void getCode(String phone){
        if(!PhoneValidator.isPhoneNumber(phone)){
            iUserView.onFail(Global.getString(R.string.phone_error));;
            return;
        }
        Staff staff =new Staff();
        staff.setPhone(phone);
        userService.getCode(staff);
    }

}
